package com.ruoyi.web.controller.basedata;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.service.IDyUserinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 大屏数据
 *
 * @author ruoyi
 * @date 2024-04-25
 */
@Api("大屏统计信息")
@RestController
@RequestMapping("/basedata/dapin")
public class DapinController extends BaseController {
    @Autowired
    private IDyUserinfoService dyUserinfoService;

    @ApiOperation("数量统计-已完成")
    @PostMapping("/fishcount")
    public R<Map<String, Integer>> fishCount(){

        Map<String, Integer> fish = new HashMap<>();
        //今日访客, 根据IP获取访客
        fish.put("visitor", dyUserinfoService.getCount(1));
        //今日
        fish.put("today", dyUserinfoService.getCount(2));
        //累计
        fish.put("total", dyUserinfoService.getCount(3));
        //待处理请求
        fish.put("pending", dyUserinfoService.getCount(4));

        return R.ok(fish);
    }

    @ApiOperation("7天图表统计-已完成")
    @PostMapping("/fishchat")
    public R<List<Map<String, Object>>> fishchat(){
        Map<String, Object> qtVlist = new HashMap<>();
        Map<String, Object> qtFlist = new HashMap<>();
        Map<String, Object> qtRq = new HashMap<>();
        List<String> riqi = this.getAscDateList();

        List<Map<String, Object>> rs = new ArrayList<>();
        qtRq.put("name","日期");
        qtRq.put("type","0");
        qtRq.put("data",riqi);
        rs.add(qtRq);

        //7天访客
        qtVlist.put("name","访客");
        qtVlist.put("type","1");
        qtVlist.put("data",this.zuHeBaoJingShuJu(riqi, dyUserinfoService.getStatisCount(1)));
        rs.add(qtVlist);

        //7天上鱼
        qtFlist.put("name","上鱼");
        qtFlist.put("type","2");
        qtFlist.put("data",this.zuHeBaoJingShuJu(riqi, dyUserinfoService.getStatisCount(2)));
        rs.add(qtFlist);

        return R.ok(rs);
    }

    private List<Integer> zuHeBaoJingShuJu(List<String> dates, List<Map<String, Object>> datas){
        List<Integer> rs = new ArrayList<>();
        for(String d : dates){
            Map tmp = new HashMap();
            Integer tmpV = 0;
            if(datas!=null && datas.size()>0){
                for(Map data: datas) {
                    if (data.get("rq").equals(d)) {
                        tmpV = Integer.valueOf(data.get("num").toString());
                    }
                }
            }
            rs.add(tmpV);
        }
        return rs;
    }

    //获取7天日期列表
    private List<String> getAscDateList() {
        Date nowDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化一下
        Calendar calendar = Calendar.getInstance();//获取对日期操作的类对象
        calendar.add(Calendar.DATE, -6);//获取7天前的日期对象
        String endDateStr = sdf.format(nowDate);
        String startDateStr = sdf.format(calendar.getTime());

        List<String> result = new ArrayList<>();
        if (endDateStr.compareTo(startDateStr) <= 0) {
            result.add(startDateStr);
            return result;
        }
        result.add(startDateStr);
        while (true) {
            calendar.add(Calendar.DATE, +1);
            String midDateStr = sdf.format(calendar.getTime());
            result.add(midDateStr);
            if (endDateStr.compareTo(midDateStr) <= 0) {
                return result;
            }
        }
    }


}
